package cn.highedu.coolshark.account.controller;

import cn.highedu.coolshark.account.pojo.dto.AdminAddNewDTO;
import cn.highedu.coolshark.account.pojo.dto.AdminListParamPagedDTO;
import cn.highedu.coolshark.account.pojo.dto.AdminLoginInfoDTO;
import cn.highedu.coolshark.account.pojo.dto.AdminUpdateDTO;
import cn.highedu.coolshark.account.pojo.vo.AdminListItemVO;
import cn.highedu.coolshark.account.pojo.vo.AdminLoginInfoVO;
import cn.highedu.coolshark.account.pojo.vo.AdminStandardVO;
import cn.highedu.coolshark.account.pojo.vo.JwtVO;
import cn.highedu.coolshark.account.security.JwtTokenHandler;
import cn.highedu.coolshark.account.security.UserDetailsImpl;
import cn.highedu.coolshark.account.service.IAdminService;
import cn.highedu.coolshark.common.JsonResult;
import cn.highedu.coolshark.common.PagedVO;
import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * 处理管理员相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "管理员用户相关接口")
@Slf4j
@RestController
@RequestMapping(path = "/admins")
public class AdminController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private JwtTokenHandler jwtTokenHandler;

    public AdminController() {
        log.debug("创建控制器类对象：AdminController");
    }

    @PostMapping("/list")
    public JsonResult<PagedVO<AdminListItemVO>> list(
            @RequestBody AdminListParamPagedDTO adminListParamPagedDTO) {
        log.debug("开始处理【查询所有管理员】的请求");
        PagedVO<AdminListItemVO> pagedVO = adminService.list(adminListParamPagedDTO);
        log.debug("查询所有管理员成功，返回结果：{}", pagedVO);
        return JsonResult.ok(pagedVO);
    }


    // http://localhost:9081/admins/add-new
    // @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    @ApiOperation("添加管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminAddNewDTO", value = "管理员信息",
                    required = true, dataTypeClass = AdminAddNewDTO.class)
    })
    @ApiResponse(code = 20000, message = "OK", response = JsonResult.class)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@RequestBody AdminAddNewDTO adminAddNewDTO) {
        log.debug("开始处理【添加管理员】的请求，参数：{}", adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/9527/delete
    // @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除管理员】的请求，参数：{}", id);
        adminService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/delete-all
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @PostMapping("/delete-all")
    public JsonResult<Void> delete(@RequestBody Long[] ids) {
        log.debug("开始处理【根据ID批量删除管理员】的请求，参数：{}", ids);
        adminService.delete(ids);
        return JsonResult.ok();
    }

    // http://localhost:9081/admins/9527/update
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable Long id, @RequestBody AdminUpdateDTO adminUpdateDTO) {
        log.debug("开始处理【根据ID修改管理员】的请求，参数：{}", adminUpdateDTO);
        adminService.update(id, adminUpdateDTO);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/{id:[0-9]+}/add-roles")
    public JsonResult<Void> addRoles(@PathVariable Long id, @RequestBody Long[] roleIds) {
        log.debug("开始处理【为管理员分配角色】的请求，参数：{}", roleIds);
        adminService.addRoles(id, roleIds);
        return JsonResult.ok();
    }


    @ApiOperation("根据id查询管理员信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "管理员id",
                    required = true, dataTypeClass = Long.class)
    )
    @ApiResponse(code = 20000, message = "OK", response = JsonResult.class)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<AdminStandardVO> getById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询管理员信息】的请求，参数：{}", id);
        AdminStandardVO admin = adminService.getAdminById(id);
        return JsonResult.ok(admin);
    }

    /**
     * 管理员启动
     * @param id 管理员id
     */
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> changeStatus(@PathVariable Long id) {
        log.debug("开始处理【管理员启用】的请求，参数：{}", id);
        adminService.changeEnable(id, 1);
        return JsonResult.ok();
    }

    /**
     * 管理员禁用
     * @param id 管理员id
     */
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> disable(@PathVariable Long id) {
        log.debug("开始处理【管理员禁用】的请求，参数：{}", id);
        adminService.changeEnable(id, 0);
        return JsonResult.ok();
    }

}
